package com.kehutong.payfees.util;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.util.Objects;

public class ContactClient {

    @Inject
    private DoveClient doveClient;

    public JSONArray getResidentHouseList(Token token, JSONObject json) throws Exception {
        JSONObject result = doveClient.post("/crm/resident/getResidentHouseList", http->{
            JSONObject body = new JSONObject();

            String residentNo = json.getString("residentNo");
            if(Objects.nonNull(residentNo)){
                body.put("residentNo", residentNo);
            }

            String houseNo = json.getString("houseNo");
            if(Objects.nonNull(houseNo)){
                body.put("houseNo", houseNo);
            }

            body.put("pageNo", 1);
            body.put("pageSize", 1000000);

            http.addHeader("companyNo", token.getCompanyNo());
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONObject getResidentHouseById(Token token, String id) throws Exception {
        JSONObject result = doveClient.post("/crm/resident/getResidentHouseById", http->{
            http.addHeader("companyNo", token.getCompanyNo());
            http.addParam("id", id);
        });

        JSONObject data = result.getJSONObject("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONObject getOwnerHouse(Token token, String houseNo) throws Exception {
        JSONObject result = doveClient.post("/crm/resident/getOwnerHouse", http->{
            http.addHeader("companyNo", token.getCompanyNo());
            http.addParam("houseNo", houseNo);
        });

        JSONObject data = result.getJSONObject("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }
}
